using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Siderys.Blog.VirtualEarth.UI.UserControl;
using Microsoft.SharePoint;
using System.ComponentModel;

namespace Siderys.Blog.VirtualEarth.WSP
{
    [Guid("87889481-C805-4e82-8314-6EF798A7C32E")]
    public class SiderysBlogVirtualEarthWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private LocationsVirtualEarth lMapaUC = null;
        private string mNombreLista = string.Empty;

        [WebBrowsable(true), Personalizable(true), Category("Siderys Blog"), DisplayName("List Map"), WebDisplayName("List Map"), Description("The list must has associated the content type SiderysVirtualEarthMap")]
        public string NombreLista
        {
            get { return mNombreLista; }
            set 
            {
                if (CheckListImplementContentType(value.ToString()))
                {
                    mNombreLista = value;
                }
                else
                {
                    throw new Exception("The list " + value.ToString() + " hasn't associated the content type SiderysVirtualEarthMap");
                }
            }
        }

        private bool CheckListImplementContentType(string pNombreLista)
        {
            bool lReturn = false;
            SPList lList = SPContext.Current.Web.Lists[pNombreLista];
            if (lList.ContentTypes["Siderys.Blog.VirtualEarthMap"] != null)
            {
                lReturn = true;
            }
            return lReturn;
        }

        public SiderysBlogVirtualEarthWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    base.CreateChildControls();
                    base.CreateChildControls();
                    lMapaUC = (LocationsVirtualEarth)Page.LoadControl(@"/_layouts/Siderys.Blog/UserControl/LocationsVirtualEarth.ascx");
                                                                          
                    lMapaUC.NombreLista = mNombreLista;

                    Controls.Add(lMapaUC);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}
